package com.waterworld.watch.home.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.SportBean;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.TimeUtils;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.interfaces.IHomeManager;
import com.waterworld.watch.home.manager.HomeManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.EventListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.waterworld.watch.push.contant.MessageContants;
import com.waterworld.watch.push.event.MessageEvent;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SkillSportActivity extends BaseActivity {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.tv_walk_count)
    TextView tv_walk_count;

    @BindView(R.id.tv_time)
    TextView tv_time;

    @BindView(R.id.btn_Refresh)
    Button btn_Refresh;

    @BindView(R.id.tv_calorie)
    TextView tv_calorie;

    @BindView(R.id.tv_mileage)
    TextView tv_mileage;


    private IHomeManager iHomeManager = HomeManager.getInstance();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skill_sport);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
    }


    private void initView(){
        setViewSize(header_parent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        header_title.setText(getString(R.string.sport_count));
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);

        getLastSportCount();

    }

    private void getSportCount() {
        if (iHomeManager != null) {
            iHomeManager.getSportsCount(new BaseObserverListener<BaseResultBean<SportBean>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(SkillSportActivity.this, getString(R.string.data_error));
                }

                @SuppressLint("SetTextI18n")
                @Override
                public void onNext(BaseResultBean<SportBean> baseResultBean){
                    if (baseResultBean.getCode() != 0) {
                        ToastUtils.showShort(SkillSportActivity.this,baseResultBean.getMsg());
                        if (baseResultBean.getData() != null) {
                            tv_walk_count.setText(baseResultBean.getData().getStepNumber().toString());
                            @SuppressLint("SimpleDateFormat") String poorHour = TimeUtils.getDatePoor(baseResultBean.getData().getUploadTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
                            tv_time.setText(String.format(getString(R.string.sport_upload), Integer.parseInt(poorHour)));
                            tv_mileage.setText(baseResultBean.getData().getDistance().toString());
                            tv_calorie.setText(baseResultBean.getData().getCal().toString());
                        }
                    } else {
                        showDialog("正在获取中...","获取成功","获取失败");
                    }
                }
            });
        }
    }

    private void getLastSportCount() {
        if (iHomeManager != null) {
            iHomeManager.getLastSportsCount(new BaseObserverListener<BaseResultBean<SportBean>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    ToastUtils.showShort(SkillSportActivity.this, getString(R.string.data_error));
                }

                @SuppressLint("SetTextI18n")
                @Override
                public void onNext(BaseResultBean<SportBean> baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        if (baseResultBean.getData() != null) {
                            tv_walk_count.setText(baseResultBean.getData().getStepNumber().toString());
                            @SuppressLint("SimpleDateFormat") String poorHour = TimeUtils.getDatePoor(baseResultBean.getData().getUploadTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
                            tv_time.setText(String.format(getString(R.string.sport_upload), Integer.parseInt(poorHour)));
                            tv_mileage.setText(baseResultBean.getData().getDistance().toString());
                            tv_calorie.setText(baseResultBean.getData().getCal().toString());
                        }
                    } else {
                        ToastUtils.showShort(SkillSportActivity.this,baseResultBean.getMsg());
                    }

                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @OnClick(R.id.btn_Refresh)
    void onRefresh() {
        getSportCount();
    }

    @OnClick(R.id.header_back)
    void onBack() {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getNewSportCount(MessageEvent event) {
        if (event.getType() == MessageContants.STEP) {
            getLastSportCount();
            closeDialog();
        }
    }
}
